#include <iostream>
#include <string>
#include "44usingClass.hpp"

UsingClass::UsingClass()
{
    company = "no name";
    shares = 0;
    share_val = 0;
    total_val = 0;
}

UsingClass::UsingClass(const std::string comp, long n, double pr)
{
    company = comp;
    if (n < 0)
    {
        std::cerr << "Number of shares can't be negative; " << company << " shares ser to 0." << std::endl;
        shares = 0;
    }
    else
    {
        shares = n;
    }
    share_val = pr;
    set_tot();
}

UsingClass::~UsingClass()
{
    // keep quiet 23333
}

void UsingClass::acquire(const std::string &co, long n, double pr)
{
    using namespace std;
    company = co;
    if (n < 0)
    {
        std::cout << "Number of shares can't be negative; "
                  << company << " shares set to 0.\n";
        shares = 0;
    }
    else
        shares = n;
    share_val = pr;
    set_tot();
}

void UsingClass::buy(long num, double price)
{
    if (num < 0)
    {
        std::cout << "Number of shares purchased can't be negative. "
                  << "Transaction is aborted.\n";
    }
    else
    {
        shares += num;
        share_val = price;
        set_tot();
    }
}

void UsingClass::sell(long num, double price)
{
    using std::cout;
    if (num < 0)
    {
        cout << "Number of shares sold can't be negative. "
             << "Transaction is aborted.\n";
    }
    else if (num > shares)
    {
        cout << "You can't sell more than you have! "
             << "Transaction is aborted.\n";
    }
    else
    {
        shares -= num;
        share_val = price;
        set_tot();
    }
}

void UsingClass::update(double price)
{
    share_val = price;
    set_tot();
}

void UsingClass::show() const
{
    // set format to #.###
    using std::cout;
    using std::ios_base;

    ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield);
    std::streamsize prec = cout.precision(3);

    // print function
    std::cout << "Company: " << company
              << "  Shares: " << shares << '\n'
              << "  Share Price: $" << share_val
              << "  Total Worth: $" << total_val << '\n';

    // restore original format
    cout.setf(orig, ios_base::floatfield);
    cout.precision(prec);
}

const UsingClass & UsingClass::top_val(const UsingClass &uc) const
{
    if (uc.total_val > total_val)
    {
        return uc;
    }
    else
    {
        return *this;
    }
}